﻿using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using ConcreteMappersAndObjects.DomainObjects;
using DataMappers;
using System.Data;


namespace ConcreteMappersAndObjects.Mappers
{
    public class CourseDataMapper : AbstractDataMapper<int,Course>
    {
        private IEntityMapper<CourseCUKey, CourseCU> _courseCUMapper;

        public void SetCourseCUDatamapper(IEntityMapper<CourseCUKey, CourseCU> courseCuDataMapper)
        {
            _courseCUMapper = courseCuDataMapper;
        }

        protected override int GetKeyFrom(SqlDataReader sdr)
        {
            return sdr.GetInt32(0);
        }

        protected override string DoGetAllStatement()
        {
            return "SELECT [ID] ,[Acronym] ,[Name] FROM [Course]";
        }

        protected override string DoGetFindStatement()
        {
            return DoGetAllStatement() + "WHERE ID = @ID";
        }

        protected override void BindFindStatement(SqlCommand cmd, int key)
        {
            cmd.Parameters.AddWithValue("@ID", key);
        }

        private class Filter : IFilter
        {
            private int _key;

            public Filter(int key)
            {
                _key = key;
            }

            public string GetWhereClause()
            {
                return "WHERE CourseID = @courseID";
            }

            public void BindValues(SqlCommand cmd)
            {
                cmd.Parameters.AddWithValue("@courseID",  _key);
            }
        }

        protected override Course DoLoad(SqlDataReader sdr)
        {

            LazySet<CourseCUKey,CourseCU> lazySet = new LazySet<CourseCUKey, CourseCU>(_courseCUMapper , new Filter(sdr.GetInt32(0)));
            return new Course(sdr.GetInt32(0),sdr.GetString(1),sdr.GetString(2), lazySet);
        }

        protected override string GetUpdateCommand()
        {
            throw new NotImplementedException();
        }

        protected override void DoBindUpdate(SqlCommand cmd, Course t)
        {
            throw new NotImplementedException();
        }

        protected override string GetInsertCommand()
        {
            return "CourseInsert";
        }

        protected override void DoBindInsert(SqlCommand cmd, Course t)
        {
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add("@Acronym", SqlDbType.NVarChar, 8).Value = t.Acronym;
            cmd.Parameters.Add("@Name", SqlDbType.NVarChar, 25).Value = t.Name;
        }

        protected override string GetRemoveCommand()
        {
            return "delete from Course where ID = @ID";
        }

        protected override void DoBindRemove(SqlCommand cmd, Course t)
        {
            cmd.Parameters.Add("@ID", SqlDbType.Int).Value = t.ID;
        }

        protected override void FinishUpdateObject(Course t)
        {
            throw new NotImplementedException();
        }

        protected override void FinishNewObject(Course t, object id)
        {
            t.ID = Convert.ToInt32(id);
        }

        protected override void FinishDeleteObject(Course t)
        {
            
        }
    }
}
